<template>
  <div id="tabs-container" class="tabs-container">
    <el-tabs
      v-model="activeTab"
      :ref="tabsRef"
      :type="type"
      :editable="editable"
      :tab-position="tabPosition"
      :stretch="stretch"
      :before-leave="leaveTab"
      @tab-remove="removeTab"
      @tab-click="tabClick"
      @edit="handleTabsEdit"
      :style="styles"
    >
      <el-tab-pane
        v-for="item in tabData"
        :key="item.name"
        :name="item.name"
        :disabled="item.disabled || disabled"
        :closable="item.closable || closable"
        :lazy="item.lazy || lazy"
      >
        <span slot="label" :class="item.class || icon">{{ item.title }}</span>
        <keep-alive>
          <component :is="item.comp" v-bind="$attrs" v-on="$listeners" />
          <!--:is 的作用：标签转换成 active 变量绑定的这个组件 -->
          <!-- () => import('@/page/mat.vue') -->
        </keep-alive>
        <slot name="tabContent"></slot>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getTabData } from "@/components/api/tab.js";
export default {
  name: "tabs",
  props: {
    //自定义ref
    tabsRef: {
      type: String,
      default() {
        return "tabs";
      },
    },
    //默认打开的页签
    active: {
      type: [String, Number],
      default() {
        return "";
      },
    },
    //页签数据
    customData: {
      type: Array,
      default() {
        return [];
      },
    },
    //风格类型 :card/border-card
    type: {
      type: String,
      default() {
        return "card";
      },
    },
    //自定义样式
    styles: {
      type: String,
      default() {
        return "";
      },
    },
    //标签是否同时可增加和关闭
    editable: {
      type: Boolean,
      default() {
        return false;
      },
    },
    stretch: {
      //标签的宽度是否自撑开
      type: Boolean,
      default() {
        return false;
      },
    },
    //方向。top/right/bottom/left
    tabPosition: {
      type: String,
      default() {
        return "top";
      },
    },
    //是否禁用
    disabled: {
      type: Boolean,
      default() {
        return false;
      },
    },
    //是否可关闭
    closable: {
      type: Boolean,
      default() {
        return false;
      },
    },
    //是否延迟渲染
    lazy: {
      type: Boolean,
      default() {
        return false;
      },
    },
    //选项卡图标
    icon: {
      type: String,
      default() {
        return "";
      },
    },
    //新加页签的数据(调用新增页签方法addTab时有效)
    tabObj: {
      type: Object,
      default() {
        return {};
      },
    },
    // 是否自定义数据
    isCustomData: {
      type: Boolean,
      default() {
        return true;
      },
    },
    // 当前组件自定义数据
    customData: {
      type: Array,
      default() {
        return [];
      },
    },
    which: {
      type: [String, Number],
      default() {
        return 1;
      },
    },
  },
  data() {
    return {
      activeTab: "", //默认打开的页签
      tabData: [],
      addTabObj: this.TabObj,
      tabIndex: 0,
    };
  },
  components: {},
  watch: {
    activeTab: function (val) {
      this.$emit("update:active", val);
    },
  },
  created() {
    this.getData();
  },
  mounted() {},
  methods: {
    getData() {
      if (this.isCustomData) {
        this.activeTab = this.active;
        this.initTabData(this.customData);
      } else {
        let params = {};
        getTabData(params)
          .then((res) => {
            this.activeTab =
              res.data && res.data.length > 0 ? res.data[0].name : "";
            this.initTabData(res.data);
          })
          .catch((err) => {
            console.log("请求失败：" + err.status + "," + err.statusText);
          });
      }
    },
    initTabData(data) {
      this.tabData = data && data.length > 0 ? data : [];
    },
    //切换标签之前的钩子，若返回 false 或者返回 Promise 且被 reject，则阻止切换。
    leaveTab(activeName, oldActiveName) {
      this.$emit("callbackLeaveTab", { activeName, oldActiveName });
    },
    //tab 被选中时触发
    tabClick(actionObj, event) {
      this.$emit("callbackTabClick", { actionObj, event });
    },
    //点击 tabs 的新增按钮或 tab 被关闭后触发
    handleTabsEdit(targetName, action) {
      //action="add"/"remove"
      if (action === "add") {
        this.addTab(this.addTabObj);
      }
      if (action == "remove") {
        this.removeTab(targetName);
      }
      this.$emit("callbackTabsEdit", targetName, action);
    },
    addTab(newTabObj) {
      if (newTabObj) {
        this.tabData.push(newTabObj);
        this.activeTab = newTabObj.name;
      }
    },
    removeTab(targetName) {
      let tabsData = this.tabData;
      let activeName = this.activeTab;
      if (activeName === targetName) {
        tabsData.forEach((tab, index) => {
          if (tab.name === targetName) {
            let nextTab = tabsData[index + 1] || tabsData[index - 1];
            if (nextTab) {
              activeName = nextTab.name;
            }
          }
        });
      }
      this.activeTab = activeName;
      this.tabData = tabsData.filter((tab) => tab.name !== targetName);
    },
  },
};
</script>